package com.qianguo.buyer.tab;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.eva.androidlibrary.api.entity.UserInfo;
import com.eva.androidlibrary.icon.FruitIcon;
import com.eva.androidlibrary.utils.IconfontUtil;
import com.eva.androidlibrary.widget.CircleImageView;
import com.qianguo.buyer.BuyerLoginActivity;
import com.qianguo.buyer.R;
import com.qianguo.buyer.base.BaseFragment;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * TAB 买家个人中心
 *
 * @author eva
 * @date 16/6/28
 */
@EFragment(R.layout.fragment_tab_buy_profile)
public class BuyerProfileFragment extends BaseFragment {

    @ViewById(R.id.buyer_profile_balance_text_view)
    TextView mTvBalance;

    @ViewById(R.id.buyer_profile_nickname_text_view)
    TextView mTvNickname;

    @ViewById(R.id.buyer_profile_avatar_image_view)
    CircleImageView mImgAvatar;

    @ViewById(R.id.buyer_profile_login_button)
    Button mBtnLogin;

    @ViewById(R.id.buyer_profile_address_icon)
    TextView mTvIconAddress;

    @ViewById(R.id.buyer_profile_invite_icon)
    TextView mTvIconInvite;


    @ViewById(R.id.buyer_profile_message_icon)
    TextView mTvIconMessage;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UserInfo user) {
        mTvNickname.setVisibility(View.VISIBLE);
        mImgAvatar.setVisibility(View.VISIBLE);
        mBtnLogin.setVisibility(View.GONE);
        refreshProfile(mApp.getUser());
    }

    @Override
    protected void initializeUI() {
        EventBus.getDefault().register(this);
        IconfontUtil.setIcon(getActivity(), mTvIconAddress, FruitIcon.ICON_LOCATION);
        IconfontUtil.setIcon(getActivity(), mTvIconInvite, FruitIcon.ICON_INVITE);
        IconfontUtil.setIcon(getActivity(), mTvIconMessage, FruitIcon.ICON_MESSAGE);
        if (mApp.isLogin()) {
            mTvNickname.setVisibility(View.VISIBLE);
            mImgAvatar.setVisibility(View.VISIBLE);
            mBtnLogin.setVisibility(View.GONE);
            refreshProfile(mApp.getUser());
        } else {
            mTvNickname.setVisibility(View.GONE);
            mImgAvatar.setVisibility(View.GONE);
            mBtnLogin.setVisibility(View.VISIBLE);
        }
    }

    private void refreshProfile(UserInfo user) {
        mImageLoader.loadImage(getActivity(), mImgAvatar, user.getAvatar(), R.mipmap.default_avatar);
        mTvNickname.setText(user.getNickname());
    }

    @Click(R.id.buyer_profile_login_button)
    void clickLogin() {
        BuyerLoginActivity.jumpToLogin(getActivity());
    }

    /**
     * 我的资产
     */
//    @Click(R.id.buyer_profile_balance_layout)
//    void clickBalance() {
//
//    }

    /**
     * 优惠券
     */
//    @Click(R.id.buyer_profile_coupon_layout)
//    void clickCoupon() {
//
//    }

    /**
     * 我的资料
     */
//    @Click(R.id.buyer_profile_info_layout)
//    void clickProfile() {
//
//    }

    /**
     * 我的地址
     */
    @Click(R.id.buyer_profile_address_layout)
    void clickAddress() {

    }

    /**
     * 我的邀请
     */
    @Click(R.id.buyer_profile_invite_layout)
    void clickInvite() {

    }

    /**
     * 我的消息
     */
    @Click(R.id.buyer_profile_message_layout)
    void clickMessage() {

    }
}
